/*
 * Copyright 2006 Kevin Wetzels 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0 
 *       
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 */
package be.roam.drest.service.youtube.xml;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import be.roam.drest.service.youtube.YouTubeException;
import be.roam.util.StringUtil;

public class YouTubeResponseHandler extends DefaultHandler {
    
    protected StringBuilder characterBuffer;
    
    private int errorCode;
    
    private String errorDescription;
    
    private boolean errorResponse;
    
    protected boolean isErrorResponse() {
        return errorResponse;
    }
    
    /* (non-Javadoc)
     * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
     */
    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if ("ut_response".equals(localName)) {
            if (!"ok".equals(attributes.getValue("status"))) {
                errorResponse = true;
            }
        } else if (errorResponse && ("code".equals(localName) || "description".equals(localName))) {
            characterBuffer = new StringBuilder();
        }
    }

    /* (non-Javadoc)
     * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
     */
    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if (errorResponse) {
            if ("code".equals(localName)) {
                errorCode = StringUtil.parseInt(getCharacters(true));
            } else if ("description".equals(localName)) {
                errorDescription = getCharacters(true);
            } else if ("ut_response".equals(localName)) {
                throw new YouTubeException(errorCode, errorDescription);
            }
        }
    }

    /* (non-Javadoc)
     * @see org.xml.sax.helpers.DefaultHandler#characters(char[], int, int)
     */
    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        if (characterBuffer != null) {
            characterBuffer.append(ch, start, length);
        }
    }
    
    protected String getCharacters(boolean clear) {
        String result = characterBuffer.toString();
        if (clear) {
            characterBuffer = null;
        }
        return result;
    }
    
}
